<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="教师姓名" prop="teacherNum">
        <el-input
          v-input-restrict
          v-model="queryParams.name"
          placeholder="请输入教师姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工号" prop="name">
        <el-input
          v-input-restrict
          v-model="queryParams.teacherNum"
          placeholder="请输入工号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        v-if="
          $store.getters.roles[0] != 'finance' &&
          $store.getters.roles[0] != 'branchAdmin'
        "
        :label="`${$branchName ? $branchName : '分院'}`"
        prop="branchId"
      >
        <el-select
          filterable
          v-model="queryParams.branchId"
          placeholder="请选择"
          clearable
        >
          <el-option label="全部" value=""></el-option>
          <el-option
            v-for="(item, index) in queryParams_branchs"
            :key="index"
            :label="item.branchName"
            :value="parseInt(item.branchId)"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          v-preventReClick
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button
          v-preventReClick
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-preventReClick
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          >编辑</el-button-->
      <!--        >-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          v-preventReClick
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
            <el-button v-preventReClick
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:post:export']"
            >导出</el-button>
          </el-col> -->
      <el-col :span="1.5">
        <el-upload
          multiple
          :http-request="handleImport"
          :show-file-list="false"
          action="#"
          accept=".xls,.xlsx"
          ref="upload"
        >
          <el-button
            v-preventReClick
            type="warning"
            plain
            icon="el-icon-folder-add"
            size="mini"
            >导入</el-button
          >
        </el-upload>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-preventReClick
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleTemplate"
          >下载模板</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工号" align="center" prop="teacherNum" />

      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          {{ scope.row.sex == 0 ? "男" : "女" }}
        </template>
      </el-table-column>
      <el-table-column label="手机号码" align="center" prop="phoneNumber">
        <template slot-scope="scope">
          <span>{{ scope.row.phoneNumber | regPhone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.idCard">{{
            scope.row.idCard | regIdCard
          }}</span>
          <span v-if="!scope.row.idCard">暂无</span>
        </template> </el-table-column
      >>
      <el-table-column
        :label="`关联${$branchName ? $branchName : '分院'}`"
        align="center"
        prop="branchName"
      />
      <el-table-column label="评价" align="center" width="160">
        <template slot-scope="scope">
          <div style="text-align: left !important">
            <el-rate
              :value="scope.row.teacherStar"
              disabled
              show-score
              text-color="#ff9900"
              score-template="{value}"
            >
            </el-rate>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_normal_disable"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-preventReClick
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            >详情</el-button
          >
          <el-button
            v-preventReClick
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-dropdown>
            <span class="el-dropdown-link">
              <el-button slot="reference" type="text" size="mini"
                >更多<i class="el-icon-arrow-down"></i
              ></el-button>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button
                  v-preventReClick
                  size="mini"
                  type="text"
                  icon="el-icon-refresh"
                  @click="handleReset(scope.row)"
                  >重置密码
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  v-preventReClick
                  size="mini"
                  type="text"
                  style="color: #e95756"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  >删除
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <addTeacher
      :open.sync="open"
      :type="type"
      :id="id"
      @refresh="getList()"
    ></addTeacher>
  </div>
</template>

<script>
import { listPost_branch } from "@/api/schoolOrganization/organization";
import addTeacher from "@/components/addTeacher.vue";
import {
  listPost_teacher,
  delPost_teacher,
  checkTeacherNum,
  import_teacher,
  resetTeacherPwd,
} from "@/api/schoolOrganization/teacher";
export default {
  name: "Post",
  components: { addTeacher },
  dicts: ["sys_normal_disable"],
  data() {
    return {
      //   分院列表
      queryParams_branchs: [],
      vuecutshow: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 是否显示弹出层
      type: 1,
      open: false,
      id: 0,
      // 查询参数
      queryParams: {
        teacherNum: undefined,
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        branchId: undefined,
      },
    };
  },
  created() {
    this.getList();
    this.getBranch();
  },

  mounted() {},
  methods: {
    handleReset(row) {
      const postIds = row.teacherId;
      this.$modal
        .confirm("确定重置密码？")
        .then(function () {
          return resetTeacherPwd(postIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("重置密码成功");
        })
        .catch(() => {});
    },
    getBranch() {
      listPost_branch({
        pageNum: 1,
        pageSize: 9999,
        schoolId: this.$store.getters.schoolId,
      }).then((response) => {
        this.queryParams_branchs = response.rows;
        this.form_branchs = response.rows;
      });
    },
    handleTemplate() {
      window.location.href = "/static/tea.xlsx";
    },
    async handleImport(file) {
      let formData = new FormData();
      formData.append("file", file.file);
      const loading = this.$loading({
        lock: true,
        text: "正在上传,请稍等…",
        spinner: "el-icon-loading",
        background: "rgba(255, 255, 255, 0.6)",
      });
      import_teacher(formData)
        .then((response) => {
          loading.close();
          this.$modal.msgSuccess("导入成功");
          this.getList();
        })
        .catch((err) => {
          loading.close();
        });
    },
    /** 组件返回图片 */
    coverResult(e) {
      this.form.teacherPic = e;
    },
    editCropper() {
      this.vuecutshow = true;
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      this.queryParams.schoolId = this.$store.getters.schoolId;
      listPost_teacher(this.queryParams).then((response) => {
        response.rows.map((item) => {
          if (item.teacherStar == undefined) {
            item.teacherStar = 0;
          }
        });
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.teacherId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /* 详情按钮操作 */
    handleDetail(row) {
      this.$router.push({
        path: "/schoolOrganization/teacherDetail",
        query: {
          teacherId: row.teacherId,
        },
      });
    },
    /** 新增按钮操作 */
    async handleAdd() {
      let res = await checkTeacherNum();
      if (res.data) {
        this.open = true;
        this.type = 1;
      } else {
        this.$modal.msgWarning("当前已达到教师数量限制");
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.id = row.teacherId;
      this.open = true;
      this.type = 2;
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.teacherId || this.ids;
      this.$modal
        .confirm("确定删除？")
        .then(function () {
          return delPost_teacher(postIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/post/export",
        {
          ...this.queryParams,
        },
        `post_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep.uploadTip {
  height: 210px;
  line-height: 390px;
}

.upload-head {
  width: 152px;
  height: 212px;

  .add {
    width: 29px;
    height: 29px;
    margin: 70px 0 0 60px;
  }

  .cover {
    width: 150px;
    height: 210px;
    border-radius: 4px;
  }
}

.upload-head:hover:after {
  line-height: 210px;
}
</style>
